package whu.cn.whushare.ui.main.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.androidplus.util.LogUtils;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.common.Constants;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.common.LoadingHelper;
import whu.cn.whushare.ui.main.known.QuestionDetailActivity;
import whu.cn.whushare.ui.main.known.adapter.AllQuestionListAdapter;
import whu.cn.whushare.ui.main.known.adapter.SearchQuestionAdapter;
import whu.cn.whushare.ui.main.known.bean.AllQuestion;
import whu.cn.whushare.ui.main.known.bean.SearchQuestion;
import whu.cn.whushare.ui.main.my.adapter.AllTopicAdapter;
import whu.cn.whushare.ui.main.my.bean.AllTopic;
import whu.cn.whushare.util.SetOnClickUtil;
import whu.cn.whushare.util.Utils;


/* Created by xiaoping on 2015/4/21.*/


@ViewMapping(id = R.layout.activity_hot_topics)
public class MyAllTopic extends BaseActivity {
    @ViewMapping(id = R.id.all_topic_list)
    private PullToRefreshListView mRefreshListView;
    private LoadingHelper mLoadingHelper;
    private int mCurrentPage =1;
    private String mTopicName;
    private String mTopicImage;
    private String mTopicId;

    private AllTopicAdapter mAdapter;
    private List<AllTopic> mTopics;

    public static Intent getStartIntent(Context context) {
        Intent i = new Intent(context, MyAllTopic.class);
        i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return i;
    }

    protected void requestTopic(int page) {
        Downloader.getInstance(this).getMyTopic(page,this,page);
        mLoadingHelper.showLoadingView(false);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();


    }


    private void initView() {
        View root = ViewMappingUtil.mapView(this, this);
        getTopBar(root);
        SetOnClickUtil.registerOnClick(this);
        mTopBar.title.setText("话题展示");
        initLoadingHelper();
        initListView();

    }

    private void initListView() {
        mRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                requestTopic(1);
                Utils.resetRefreshLabel(MyAllTopic.this, mRefreshListView);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                if (mTopics.size() >= mTotal) {
                    Utils.setRefreshLabelToLast(MyAllTopic.this, mRefreshListView);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mRefreshListView.onRefreshComplete();
                        }
                    }, 500);
                } else {
                    requestTopic(++mCurrentPage );

                }
            }
        });
        mRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AllTopic allTopic = (AllTopic)mAdapter.getItem(position - 1);
                mTopicName=allTopic.getTopics().topicName;
                mTopicImage=allTopic.getTopics().topicImage;
                mTopicId=allTopic.getTopics().id;
                startActivityForResult(MyTopicDetailActivity.getStartIntent(MyAllTopic.this,mTopicName,mTopicImage,mTopicId), 1);
            }

        });

   }




    private void initLoadingHelper() {
        mLoadingHelper = new LoadingHelper(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               requestTopic(mCurrentPage);
            }
        });
        mLoadingHelper.onCreateView(LayoutInflater.from(this), mRefreshListView);
        requestTopic(mCurrentPage);
    }


    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);
        mCurrentPage= (int) obj;

        switch (type) {
            case MY_TOPIC:
                List<AllTopic> topics = (List<AllTopic>) response;

                if (mTopics == null || mCurrentPage == 1) {
                    mTopics = topics;

                    mAdapter = new AllTopicAdapter(this,mTopics);
                    mRefreshListView.setAdapter(mAdapter);
                } else {
                    mTopics.addAll(topics);
                    mAdapter.notifyDataSetChanged();
                }

                mLoadingHelper.showContentView();
                mRefreshListView.onRefreshComplete();

                break;
            default:
                break;
        }

    }

    @Override
    public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
        super.onFail(type, error);
        mLoadingHelper.showRetryView();

    }


}

